package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.avic.rong.R;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserInfoCompleteActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webview)
    BridgeWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_complete);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
        }

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }

        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\", "");
                JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                if (currentUrl.contains("goods/myDownload")){//下载
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(currentUrl);
                    intent.setData(content_url);
                    startActivity(intent);
                }
            }
        });


        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

        //修改密码成功
        webView.registerHandler("updatePassword", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

        //保存成功
        webView.registerHandler("popVCReload", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

    }


    public static Intent queryIntent(Context context,String url){
        Intent intent = new Intent(context,UserInfoCompleteActivity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }
}
